package net.tngou.iask.action.my;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.entity.Td;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Answer;
import net.tngou.iask.pojo.Disease;
import net.tngou.iask.pojo.Filter;
import net.tngou.iask.pojo.Question;
import net.tngou.iask.pojo.Survey;
import net.tngou.iask.pojo.Survey_question;
import net.tngou.iask.service.AnswerService;
import net.tngou.iask.service.DiseaseService;
import net.tngou.iask.service.QuestionService;
import net.tngou.iask.service.SurveyService;
import net.tngou.iask.util.StringUtil;

/**
 * 问卷
 * @author tngou@tngou.net
 *
 */
public class SurveyAction extends BaseAction {

	/**
	 * 列表
	 */
	public void list() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		int type=(int) ask.getId();
		SurveyService service = new SurveyService();
		PageUtil page = service.getPage(ask.getPage(), ask.getSize() , user.getMain(), type, ask.getKeyword());
		root.put("page", page);
		root.put("type", type);
		printFreemarker("my/survey_list.ftl", root);
		
		
	}
	/**
	 * 添加
	 */
	public void add() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		int type=(int) ask.getId();
		root.put("type", type);
		printFreemarker("my/survey_add.ftl", root);
	}
	
	/**
	 * 更新
	 */
	public void update() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		long id=ask.getId();
		if(id==0){run_404();return;}
		Survey survey = new Survey().get(id);
		if(survey==null){run_404();return;}
		root.put("type", survey.getType());
		root.put("survey", survey);
		printFreemarker("my/survey_update.ftl", root);
	}
	/**
	 * 显示
	 */
	public void show() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		String[] params = request.getParams();
		int id=0;
		if(params!=null&&params.length>0&&NumberUtils.isNumber(params[0]))
		{	
			id=Integer.parseInt(params[0]);		
		}
		if(id==0){run_404();return;}
		Survey survey = new Survey().get(id);
		if(survey==null){run_404();return;}
//		DiseaseService diseaseService = new DiseaseService();
//		if(survey.getType()==2)
//		{			
//			List<Disease> diseases = diseaseService.getDisease(survey.getId());		
//			root.put("diseases", diseases);
//		}
		
		SurveyService service = new SurveyService();
		List<List<Td>> trs = service.getTrs(survey, null,true);
		root.put("trs", trs);
		root.put("survey", survey);
		printFreemarker("my/survey_show.ftl", root);
	}
	/**
	 * 修改问题顺序
	 */
	public void question() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		int id=(int) ask.getId();
		if(id==0){run_404();return;}
		Survey survey = new Survey().get(id);
		if(survey==null){run_404();return;}
		
		QuestionService questionService = new QuestionService();

		List<Map<String, Object>> list = questionService.getQuestionMap(survey.getId());
		root.put("list", list);
		root.put("survey", survey);

		printFreemarker("my/survey_question.ftl", root);
	}
	public void disease() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		int id=(int) ask.getId();
		if(id==0){run_404();return;}
		Survey survey = new Survey().get(id);
		if(survey==null){run_404();return;}
		
		DiseaseService diseaseService = new DiseaseService();
		List<Map<String, Object>> list = diseaseService.getDisease(survey.getId());
		root.put("list", list);
		root.put("survey", survey);
		printFreemarker("my/survey_disease.ftl", root);
	}
	
	
	@Deprecated
	public void questionseq() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		int id=(int) ask.getId();
		if(id==0){run_404();return;}
		Survey_question survey_question= new Survey_question().get(id);
		if(survey_question==null){run_404();return;}
		Survey survey = new Survey().get(survey_question.getSurvey());
		Question question =new Question().get(survey_question.getQuestion());
		if(survey==null||question==null){run_404();return;}
		root.put("survey", survey);
		root.put("question", question);
		root.put("survey_question", survey_question);
		printFreemarker("my/survey_questionseq.ftl", root);
	}
	
	/**
	 * 添加问题
	 */
	public void questionadd() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		int id=(int) ask.getId();
		if(id==0){run_404();return;}
		Survey survey = new Survey().get(id);
		if(survey==null) {run_404();return;}
		QuestionService service = new QuestionService();
		PageUtil page = service.getPageNotQuestion(ask.getPage(),ask.getSize(),user.getMain(),survey.getId(),ask.getKeyword());
		root.put("survey", survey);
		root.put("page", page);
		printFreemarker("my/survey_questionadd.ftl", root);
	}
	
	public void diseaseadd() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		int id=(int) ask.getId();
		if(id==0){run_404();return;}
		Survey survey = new Survey().get(id);
		if(survey==null) {run_404();return;}
		DiseaseService service = new DiseaseService();
		PageUtil page = service.getPage(ask.getPage(),ask.getSize(),user.getMain(),survey.getId(),ask.getKeyword());
		root.put("survey", survey);
		root.put("page", page);
		printFreemarker("my/survey_diseaseadd.ftl", root);
	}
}
